<template>
<div class=''>
    <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片结构 -->
    <el-card class="box-card">
       <!--搜索与添加区域-->
       <el-row :gutter="20">
           <el-col :span="6">
            <el-input placeholder="请输入内容" v-model="input3" class="input-with-select" clearable @clear="getDataList">
                <el-button slot="append" icon="el-icon-search" @click="searchBtn"></el-button>
            </el-input>
           </el-col>
            <el-button type="primary" @click.native="addUserBtn">添加用户</el-button>
       </el-row>
       <!-- 用户列表区 -->
       <template>
           <el-table
            :data="userList"
            :header-cell-style="{background:'#F3F4F7',color:'#555'}"
            border
            stripe
            style="width: 100%">
                <el-table-column
                    type="index"
                    label="序号"
                    width="180">
                </el-table-column>
                <el-table-column
                    :filters="[{text:'2016-05-01'},{text:'2016-05-01'},{text:'2016-05-01'},{text:'2016-05-01'}]"
                    prop="username"
                    label="姓名"
                    width="180">
                </el-table-column>
                <el-table-column
                    prop="mobile"
                    label="电话"
                    width="180">
                </el-table-column>
                <el-table-column
                    prop="email"
                    label="email">
                </el-table-column>
                <el-table-column
                    prop="email"
                    label="email">
                </el-table-column>
                <el-table-column
                    label="状态">
                    <template slot-scope="scope">
                        <el-switch
                            v-model="scope.row.mg_state"
                            @change="changeFn">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column
                filter-multiple
                    prop="role_name"
                    label="操作">
                    <template>
                        <el-tooltip class="item" effect="dark" content="编辑" placement="top" :enterable="false" >
                            <el-button type="primary" icon="el-icon-edit" circle @click="onEditFn"></el-button>
                        </el-tooltip>
                        <el-tooltip class="item" effect="dark" content="删除" placement="top" :enterable="false">
                            <el-button type="danger" icon="el-icon-delete" circle @click="onDel"></el-button>
                        </el-tooltip>
                        <el-tooltip class="item" effect="dark" content="分配角色" placement="top" :enterable="false">
                            <el-button type="warning" icon="el-icon-setting" circle @click="assigningRoles"></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage4"
                :page-sizes="[100, 200, 300, 400]"
                :page-size="100"
                layout="total, sizes, prev, pager, next, jumper"
                :total="400">
            </el-pagination>
            <!-- 添加用户的对话框 -->
            <el-dialog
                :title="this.add ? '添加用户' : '修改用户'"
                :visible.sync="dialogVisible"
                width="30%"
                @close="addUserDialogClosed"
                :before-close="handleClose">
                <el-form :model="ruleFormUser" :rules="this.add ? addUserRules : editUserRules" ref="ruleFormUser" label-width="80px" class="demo-ruleForm">
                    <el-form-item label="用户名" prop="username">
                        <el-input v-model="ruleFormUser.username" :disabled="this.add ? false : true"></el-input>
                    </el-form-item>
                    <el-form-item label="密  码" prop="password" v-if="this.add">
                        <el-input v-model="ruleFormUser.password"></el-input>
                    </el-form-item>
                    <el-form-item label="邮  箱" prop="email">
                        <el-input v-model="ruleFormUser.email"></el-input>
                    </el-form-item>
                    <el-form-item label="手  机" prop="mobile">
                        <el-input v-model="ruleFormUser.mobile"></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="dialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="addUser">确 定</el-button>
                </span>
            </el-dialog>
            <el-dialog
                title="分配角色"
                :visible.sync="assigningRolesHide"
                width="30%">
                <div>
                    当前用户名称：admin
                </div>
                <div>
                    当前用户角色：主管
                </div>
                <div style="margin-top: 15px;">
                    分配新角色：<el-select v-model="selectRole" placeholder="请选择角色" @change="selectRoleFn">
                        <el-option v-for='item in select' :label="item.name" :value="item.id" :key="item.id"></el-option>
                    </el-select>
                </div>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="assigningRolesHide = false">取 消</el-button>
                    <el-button type="primary" @click="editUser">确 定</el-button>
                </span>
            </el-dialog>
       </template>
    </el-card>
</div>
</template>
<script>
export default {
name: '',
data(){
    // 验证邮箱的规则
     let checkEmail = (rule, value, callback) => {
         // 验证邮箱的正则
        const regEmail = /^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/
        if(regEmail.test(value)){
            return callback()
        }
        callback(new Error('请输入正确的邮箱'))
      };
    // 验证手机的规则
    let checkMobile = (rule, value, callback) => {
        // 验证手机号
         const regMobile = /^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/
        if(regMobile.test(value)){
            return callback()
        }
        callback(new Error('请输入正确的手机号'))
      };
    return {
        input3:'',
        userList:[
            {
                id:'1562',
                username:'wuhan1',
                mobile:'15102725852',
                type:1,
                email:'515018930@qq.com',
                create_time:'2021-05-25',
                mg_state: true,
                role_name: '鸡蛋管理员'
            },
            {
                id:'1562',
                username:'wuhan2',
                mobile:'15102725852',
                type:1,
                email:'515018930@qq.com',
                create_time:'2021-05-25',
                mg_state: false,
                role_name: '鸡蛋管理员'
            },
            {
                id:'1562',
                username:'wuhan3',
                mobile:'15102725852',
                type:1,
                email:'515018930@qq.com',
                create_time:'2021-05-25',
                mg_state: true,
                role_name: '鸡蛋管理员'
            },
            {
                id:'1562',
                username:'wuhan4',
                mobile:'15102725852',
                type:1,
                email:'515018930@qq.com',
                create_time:'2021-05-25',
                mg_state: false,
                role_name: '鸡蛋管理员'
            }
        ],
        total: 0,
        currentPage4:1,
        dialogVisible: false,
        ruleFormUser:{
            username: '',
            password: '',
            email: '',
            mobile: ''
        },
        add: true,
        addUserRules:{
           username: [
                { required: true, message: '请输入姓名', trigger: 'blur' },
                { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
            ],
            password: [
                { required: true, message: '请输入密码', trigger: 'blur' },
                { min: 3, max: 15, message: '长度在 3 到 5 个字符', trigger: 'blur' }
            ],
            email: [
                { required: true, message: '请输入密码', trigger: 'blur' },
                { validator: checkEmail, trigger: 'blur' }
            ],
            mobile: [
                { required: true, message: '请输入密码', trigger: 'blur' },
                { validator: checkMobile, trigger: 'blur' }
            ]
        },
        editUserRules:{
            email: [
                { required: true, message: '请输入密码', trigger: 'blur' },
                { validator: checkEmail, trigger: 'blur' }
            ],
            mobile: [
                { required: true, message: '请输入密码', trigger: 'blur' },
                { validator: checkMobile, trigger: 'blur' }
            ]
        },
        assigningRolesHide: false,
        // 需要被分配角色的用户信息
        userInfo: {},
        select:[
            {
                name:'wuhan1',
                id:1
            },
            {
                name:'wuhan2',
                id:2
            },
            {
                name:'wuhan3',
                id:3
            },
            {
                name:'wuhan4',
                id:4
            }
        ],
        selectRole:''
    }
},
created(){
    // this.getUserList()
},
methods:{
    searchBtn(){
        console.log(this.input3)
        },
    changeFn(event){
        console.log(event)
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize){
        console.log(newSize)
    },
    // 监听页码值改变的事件
    handleCurrentChange (newPage) {
        console.log(newPage)
    },
    // 获取用户列表
    async getDataList () {
        await this.$http.get('/sdddd/dddd')
    },
    handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
    },
    //监听对话框的关闭事件
    addUserDialogClosed(){
        this.$refs.ruleFormUser.resetFields()
    },
    addUserBtn(){
        this.add = true
        this.dialogVisible = true
        this.$refs.ruleFormUser.resetFields()
    },
    addUser(){
            this.$refs.ruleFormUser.validate((valid) => {
            if (valid) {
                
                this.dialogVisible = false
                if(this.add){
                    // 调用数据提交接口
                    console.log(this.ruleFormUser)
                    // 数据提交成功之后关闭对话框 发起添加用户的数据请求
                    
                    this.$message({
                        message: '添加用户成功',
                        type: 'success'
                    })
                    return 
                }
                // 发起修改用户的数据请求
                this.$message({
                        message: '修改用户成功',
                        type: 'success'
                    })
            } else {
                console.log('error submit!!');
                return false;
            }
            });
    },
    onEditFn(){
        this.add = false
        this.dialogVisible = true
    },
    // 删除
    onDel(){
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    assigningRoles(){
        this.assigningRolesHide = true
    },
    selectRoleFn (){
        console.log(this.selectRole)
    },
    editUser(){
        // 发送请求 修改用户角色
    }
}
}
</script>
<style lang='less' scoped>
.el-table{
    margin-top: 15px;
}
.el-pagination{
    margin-top: 15px;
    text-align: center;
}
</style>